<template>
	<el-dialog title="修改别名" v-model="visible" :close-on-click-modal="false" draggable width="500">
		<el-form ref="dataFormRef" :model="form" :rules="dataRules" formDialogRef label-width="90px" v-loading="loading">
			<el-row :gutter="24">
				<el-col :span="22" class="mb20">
					<el-form-item label="IMEI别名" prop="imeiAlias">
						<el-input v-model="form.imeiAlias" placeholder="别名最多20个字，且不能输入空格" maxlength="20" show-word-limit />
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="visible = false">取消</el-button>
				<el-button type="primary" @click="onSubmit" :disabled="loading">确认</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup lang="ts" name="ImeiAlias">
import { useMessage } from '/@/hooks/message';
import { putObj } from '/@/api/platform/basicDevice';
import { rule } from '/@/utils/validate';
const emit = defineEmits(['success']);

// 定义变量内容
const dataFormRef = ref();
const visible = ref(false);
const loading = ref(false);
const type = ref('');
// 定义字典

// 提交表单数据
const form = reactive({
	id: '',
	imeiAlias: '',
});

// 定义校验规则
const dataRules = ref({
	imeiAlias: [{ validator: rule.checkSpaceInline, trigger: 'blur' }],
});

// 打开弹窗
const openDialog = (id: string, imeiAlias: string) => {
	visible.value = true;
	form.id = id;
	form.imeiAlias = imeiAlias;

	// 重置表单数据
	nextTick(() => {
		dataFormRef.value?.resetFields();
	});
};

// 提交
const onSubmit = async () => {
	const valid = await dataFormRef.value.validate().catch(() => {});
	if (!valid) return false;

	try {
		loading.value = true;
		await putObj(form);
		useMessage().success(`修改成功！`);
		visible.value = false;
		emit('success', form.imeiAlias);
	} catch (err: any) {
		useMessage().error(err.msg);
	} finally {
		loading.value = false;
	}
};

// 暴露变量
defineExpose({
	openDialog,
});
</script>
